package cn.hx.controller.front.user;

import cn.hx.entity.Data;
import cn.hx.entity.News;
import cn.hx.service.NewsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 新闻(News)表控制层
 *
 * @author makejava
 * @since 2022-08-17 11:15:46
 */
@Controller
public class NewsController1 {
    /**
     * 服务对象
     */
    @Resource
    private NewsService newsService;

    @GetMapping("user/news/queryList")
    @ResponseBody
    public Map queryList(@RequestParam(defaultValue = "1") Integer page, News departInfo) {
        HashMap<String, Object> map = new HashMap<>();
        List<News> news = newsService.queryAllByLimit1(departInfo, (page - 1) * 10, 10);
        map.put("list", news);


        int count = newsService.count();
        int totalPage = 0;
        if (count % 10 == 0) {
            totalPage = count / 10;
        } else {
            totalPage = count / 10 + 1;
        }
        int total = count;
        map.put("page", page);
        map.put("count", count);
        map.put("totalPage", totalPage);
        map.put("total", total);
        return map;
    }
    @GetMapping("user/newsDetail")
    public ModelAndView newsDetail(Integer id) {
        ModelAndView modelAndView = new ModelAndView("user/newsDetail");
        News staffInfo = newsService.queryById(id);

        modelAndView.addObject("data", staffInfo);
        return modelAndView;
    }
    @GetMapping("user/newsDetail1")
    @ResponseBody
    public Map newsDetail1(Integer id) {
        HashMap<Object, Object> map = new HashMap<>();
        News staffInfo = newsService.queryById(id);


        map.put("data", staffInfo);
        map.put("code", 0);
        return map;
    }
    @Value("${web.upload-path}")
    private String path;

    @CrossOrigin
    @RequestMapping("user/uploadNewsImg")
    @ResponseBody
    public Map up(MultipartFile file) throws IOException {
        System.out.println(12);
        HashMap<Object, Object> map = new HashMap<>();
        if (file==null||file.getSize()<=0) {
            map.put("errno", 1);
            map.put("msg", "文件空异常");
            return map;
        }

        File file1 = new File(path + "/news/");
        if (!file1.exists()) {
            file1.mkdirs();
        }

        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        String s = UUID.randomUUID().toString().replaceAll("-", "");
        file.transferTo(new File(file1,s+substring));
        map.put("errno", 0);
        Data data = new Data();
        data.setUrl("/news/" + s + substring);
//        new String[]{"http://192.168.8.143:8080/images/" + s + substring}
        map.put("data", data);
        return map;
    }
}